package groupC.cookassistent;

import java.io.BufferedWriter;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import de.dailab.clos.beans.AbstractServletAssistantBean;
import de.dailab.clos.onto.AssistantFact;


/**
 * AssistantBean providing the Assistant 'Smart Nutrition Assistant -
 * Cookassistant'
 * 
 * When running, visit http://localhost:8095/cookassistent/
 * 
 * @author Group C
 */
public class CookassistentAssistantBean extends AbstractServletAssistantBean {

	@Override
	protected AssistantFact createAssistant() {
		String[] optionalDevices = { "http://www.dai-labor.de/~masp/Context-1.3.ecore//Oven" };
		String[] requiredDevices = {};
		String[] exclusiveDevices = {};

		// start MaryServer TODO: toggle comment
//		try {
//			Process process = Runtime.getRuntime()
//					.exec(new String[] { "/home/florian/MARY TTS/bin", "-c",
//							"maryserver.sh" });
//		} catch (IOException e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		}

		return new AssistantFact(
				"cookassistent",
				"Smart Nutrition Assistant - Cookassistant",
				"A Servlet that helps te youser at the boil.",
				"0.1",
				"Group C",
				readImageFromResource("/groupC/cookassistent/icons/cookassistent.png"),
				new ArrayList<String>(Arrays.asList(requiredDevices)),
				new ArrayList<String>(Arrays.asList(optionalDevices)),
				new ArrayList<String>(Arrays.asList(exclusiveDevices)),
				thisAgent.getAgentDescription());
	};

	@Override
	protected Map<String, HttpServlet> createServlets() {
		Map<String, HttpServlet> servlets = new HashMap<String, HttpServlet>();
		servlets.put("", new CookassistentServlet());
		servlets.put("icon", new ResourceServlet(
				"/groupC/cookassistent/icons/", "image/png"));
		return servlets;
	}

	/**
	 * Servlet for showing Smart Nutrition Assistant - Cookassistant Web GUI
	 * 
	 * @author Group C
	 */
	protected class CookassistentServlet extends HttpServlet {

		/**
		 * 
		 */
		private static final long serialVersionUID = 5815774842470311109L;

		@Override
		protected void doGet(HttpServletRequest request,
				HttpServletResponse response) throws ServletException,
				IOException {
			// to access parameters: String id = request.getParameter("id");

			BufferedWriter buff = new BufferedWriter(new OutputStreamWriter(
					response.getOutputStream()));
	
			buff.append("<html><head><title>Smart Nutrition Assistant - Cookassistant assistant</title>"
					+ STYLE + "</head><body>");
			// TODO put your dynamic HTML code here
			buff.append("<h1>Smart Nutrition Assistant - Cookassistant</h1>");
			buff.append("<p>It works!</p>");
			buff.append("<img src=\"icon?file=cookassistent.png\"></img>");
			buff.append("</body></html>");
			buff.flush();
			buff.close();
			response.setContentType("text/html; charset=UTF-8");
			response.flushBuffer();
		};
		
		@Expose
		public void swipeRight(){
			//TODO: Action swipeRight
		};
		
		@Expose
		public void swipeLeft(){
			//TODO: Action swipeLeft
		};
		
		@Expose
		public void swipeUp(){
			//TODO: Action swipeUp
		};
		
		public void swipeDown(){
			//TODO: Action swipeDown
		};

	}
}
